<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response;
class AdminController extends BaseController {

	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/
	/**
	 * The layout that should be used for responses.
	 */
	protected $layout = 'layouts.admin';

	public function showAdmin()
	{
// 		return View::make('hello');
		$this->layout->content = View::make('admin.home');
		$type = Input::get("type",1);
		//$this->layout->title = "蔓茂花廊后台管理";
// 		$banners = Banner::orderBy('id', 'DESC')->get();
		$banners = Banner::where('type', '=', $type)->orderBy('id', 'DESC')->get();
		//$this->layout->with("title","蔓茂花廊后台管理");
		$this->layout->content->with("banners",$banners);
		$style = File::get(storage_path("style"));
		$styles = Config::get("mao.home_style");
		$this->layout->content->with("style",$style);
		$this->layout->content->with("styles",$styles);
		$this->layout->content->with("type",$type);
	}
	
	public function showLogin(){
		//Log::info("session-data-test:".Session::get("email"));
		$this->layout = View::make("admin.login")->with("email",Session::get("email"));
		$this->layout->with("title","蔓茂花廊后台管理");
		//$this->layout-> = "蔓茂花廊后台管理";
	}
	
	public function postHomeStyle(){
		$style = Input::get("style");
		File::put(storage_path("style"),$style);
	}
	
	public function login(){
		//$email = Input::get('email');
		//$password = Input::get('password');
		$user = Input::all();
		$validator = Validator::make(
				$user,
				array(
						'email' => 'required | email',
						'password' => 'required',
				)
		);
		$email = $user["email"];
		$password = $user["password"];
		if ($validator->fails()){
			//Log::info($validator->messages());
			//return $validator->messages();
			//Log::info($user);
			return Redirect::route('showlogin')->with('email',$email).with("errorMsg",'Email或密码错误');
		} else {
			//$password = Hash::make($password);
// 			Auth::attempt(array('email' => $email, 'password' => $password, 'active' => 1)
			if (Auth::attempt(array('email' => $email,'password' => $password,'active'=>1))){
				return Redirect::route('admin');
			} else {
				//$queries = DB::getQueryLog();
				//Log::info($queries);
				return Redirect::route('showlogin')->with('email',$email)->with("errorMsg",'Email或密码错误');
			}
		}
		//Input::get("email");
		//Log::info("email:".$email.",password:".$password);
	}
	
	public function logout(){
		$user = Auth::user();
		Auth::logout();
		return Redirect::route('showlogin')->with('email',$user->email);
	}
	
	/**
	 * 添加首页大图
	 */
	public function postBanner(){
		$file = Input::file('image');
		//Image::make()->save($name);
// 		$res = Images::storeBannerImage($file);
		$image = Images::storeImage($file);
		
		//图片保存成功
		if (!empty($image)){
			$url = Input::get("url",null);
			$type = Input::get('type');
			$banner = Banner::create(array('image' => $image,'url'=>$url,'type'=>$type));
// 			$res["id"] = $banner->id;
// 			$res["url"] = $url;
			return Response::json(array(
				'id' => $banner->id,
				'image' => $banner->image,
				'success' => true
			));
		}
	}
	
	/**
	 * 删除首页的大图
	 */
	public function deleteBanner(){
		$id = Input::get("id");
		$banner = Banner::find($id);
		Images::delete($banner->image);
		$banner->delete();
		return Response::json(array('success'=>true,'id'=>$id));
		//return Response::json(array('success'=>false));
	}
	
	/**
	 * 显示用户列表
	 */
	public function showUsers(){
		$query = User::orderBy('id','desc');
		$name = Input::get("name");
		$search = array();
		if (!empty($name)){
			$query->where('name','like','%'.$name.'%');
			$search["name"] = $name;
		}
		$mobile = Input::get("mobile");
		if (!empty($mobile)){
			$query->where('mobile','like','%'.$mobile.'%');
			$search["mobile"] = $mobile;
		}
		$email = Input::get("email");
		if (!empty($email)){
			$query->where('email','like','%'.$email.'%');
			$search["email"] = $email;
		}
		//paginate会自动处理分页数字
		$users = $query->paginate(50);
		// 		$products = Products::withTrashed()->orderBy('id','desc')->forPage(50,2)->get();
		$content = View::make('admin.users');
		$this->layout->content = $content;
		$content->with("users",$users);
		$content->with("search",$search);
	}
	
	public function showUserDetail($id){
		//$id = Input::get("id");
		
		$user = User::with("addresses")->find($id);
		$content = View::make('admin.user-detail');
		$this->layout->content = $content;
		$content->with("user",$user);
	}
	
	public function toggleUser(){
		$id = Input::get("id",0);
		if (empty($id)){
			return Response::json(array('success'=>false,'error'=>'没有用户编号'));
		}
		$user = User::find($id);
		if (empty($user->active)){
			$user->active = 1;
		} else {
			$user->active = 0;
		}
		$user->save();
		return Response::json(array('success'=>true));
	}
	
	public function showNotices(){
		$notices = Notice::orderBy('id','desc')->paginate(50);
		// 		$products = Products::withTrashed()->orderBy('id','desc')->forPage(50,2)->get();
		$content = View::make('admin.notice.notices');
		$this->layout->content = $content;
		$content->with("notices",$notices);
	}
	
	public function showNoticeAdd(){
		$content = View::make('admin.notice.notice-add');
		$this->layout->content = $content;
	}
	public function postNotice(){
		$n = Input::all();
		Notice::create($n);
		return Redirect::route("notice");
	}
	
	public function showNoticeUpdate($id){
		$content = View::make('admin.notice.notice-update');
		$this->layout->content = $content;
		$notice = Notice::find($id);
		$content->with("notice",$notice);
	}
	public function updateNotice(){
		$n = Input::all();
		Notice::find($n['id'])->update($n);
		return Redirect::route("notice");
	}
	
	public function deleteNotice(){
		/**/
		$id = Input::get("id");
		Notice::destroy($id);
		return Response::json(array("success"=>true));
	}
}
